package com.tduck.cloud.dashboard.service.charts;


import cn.hutool.core.map.MapUtil;
import com.tduck.cloud.dashboard.constant.DashboardConstants;
import com.tduck.cloud.dashboard.request.ChartDataRequest;
import com.tduck.cloud.report.data.params.QueryFieldListParams;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.map.LinkedMap;
import org.springframework.stereotype.Component;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * 表格数据
 *
 * @author tduck
 */
@Slf4j
@Component(value = DashboardConstants.DASHBOARD_CHART_PREFIX + "TableChartDataService")
public class TableChartDataService extends BaseDashboardChartDataService {
    private static final String HEADER = "header";
    private static final String DATA = "tableData";


    @Override
    public Map<String, Object> getChartData(ChartDataRequest request) {
        QueryFieldListParams queryFieldListParams = new QueryFieldListParams();
        queryFieldListParams.setQueryObjName(request.getIndexName());
        queryFieldListParams.setLimit(request.getLimit());
        queryFieldListParams.setFields(request.getIndex().stream().map(ChartDataRequest.IndexField::getId).collect(Collectors.toList()));
        List<LinkedMap> list = dataSearchTemplate.queryFieldList(queryFieldListParams);
        Map<String, Object> result = MapUtil.of(DATA, list);
        result.put(HEADER, request.getIndex().stream().collect(Collectors.toMap(ChartDataRequest.IndexField::getId, ChartDataRequest.IndexField::getLabel, (k1, k2) -> k1)));
        return result;
    }
}
